package com.web.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.web.entity.NoteInfo;
import com.web.entity.NoteType;
import com.web.entity.Token;
import com.web.mapper.NoteInfoMapper;
import com.web.param.DelNoteInfo;
import com.web.param.InsertNoteInfoDto;
import com.web.param.UpdateInfoDto;
import com.web.service.INoteInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.web.service.INoteTypeService;
import com.web.util.LoginException;
import com.web.util.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 笔记表 服务实现类
 * </p>
 *
 * @author hgjxyz
 * @since 2020-11-18
 */
@Service
public class NoteInfoServiceImpl extends ServiceImpl<NoteInfoMapper, NoteInfo> implements INoteInfoService {

    @Autowired private TokenServiceImpl tokenService;
    @Autowired private INoteTypeService typeService;

    @Override
    public void insert(InsertNoteInfoDto dto) throws ServiceException, LoginException {
        Token token = this.tokenService.getByToken(dto.getToken());
        NoteType type = this.typeService.get(dto.getTypeId());
        if(type == null || !type.getUserId().equals(token.getId()))throw new ServiceException("未找到当前分类");
        NoteInfo info = new NoteInfo();
        info.setId(UUID.randomUUID().toString());
        info.setTitle(dto.getTitle());
        info.setNoteTypeId(type.getId());
        info.setUserId(token.getId());
        info.setContent(null);
        info.setIsDelete(0);
        info.setCreatedTime(new Date());
        if(!this.insert(info))throw new ServiceException("新增失败");
    }

    @Override
    public void update(UpdateInfoDto dto) throws ServiceException, LoginException {
        Token token = this.tokenService.getByToken(dto.getToken());
        NoteInfo info = this.selectById(dto.getId());
        if(info == null || info.getIsDelete() == 1) throw new ServiceException("未找到当前记录");
        if(!info.getUserId().equals(token.getId())) throw new ServiceException("未找到当前记录");
        info.setContent(dto.getContent());
        if(!this.updateById(info))throw new ServiceException("修改失败");
    }

    @Override
    public void delete(DelNoteInfo dto) throws ServiceException, LoginException {
        Token token = this.tokenService.getByToken(dto.getToken());
        NoteInfo info = this.selectById(dto.getId());
        if(info == null || info.getIsDelete() == 1 || !info.getUserId().equals(token.getId())) throw new ServiceException("未找到当前记录");
        info.setIsDelete(1);
        if(!this.updateById(info)) throw new ServiceException("删除失败");
    }

    @Override
    public List<NoteInfo> getByList(String tokenStr, String typeId) throws LoginException {
        Token token = this.tokenService.getByToken(tokenStr);
        Wrapper<NoteInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",  token.getId())
                .eq("note_type_id", typeId)
                .eq("is_delete",0)
                .orderBy("created_time", false);
        return this.selectList(wrapper);
    }
}
